<!-- 宠物服务建议周期配置 -->
<template>
    <div >
        <div class='table-page-search-wrapper'>
            <a-form layout='inline'>
               <a-row :gutter='48'>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="种类">
                            <a-select v-model="speciesId" placeholder="请选择" allowClear @change="getData">
                                <a-select-option v-for="(item,i) in specList" :key="i" :value="item.id">{{item.name}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="服务项目">
                            <a-select v-model="serviceProjectId" placeholder="请选择" allowClear @change="getData">
                                <a-select-option v-for="(item,i) in projectList" :key="i" :value="item.id">{{item.name}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="状态">
                            <a-select v-model="status" placeholder="请选择" allowClear @change="getData">
                                <a-select-option v-for="(item,i) in statusList" :key="i" :value="item.id">{{item.name}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md='16' :sm='24'>
                        <a-form-item label="建议周期">
                            <div class="flex alignCenter">
                                <a-input v-model="suggestCycle" allow-clear placeholder="请输入周期时长" />
                                <div style="margin: 0 20px">-</div>
                                <a-select v-model="timeUnit" placeholder="请选择时长单位" allowClear @change="getData">
                                    <a-select-option v-for="(item,i) in timeUnitList" :key="i" :value="item.id">{{item.name}}</a-select-option>
                                </a-select>
                            </div>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item >
                            <a-button type='primary' style="margin-right: 20px" @click="getData">查询</a-button>
                            <a-button type="" icon="plus" @click="add()">添加</a-button>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>  

        <a-table rowKey="id" :loading="isload" :columns="columns" :data-source="dataList" @change="handleTableChange" :pagination="{ pageSize: 10, total: total ,showSizeChanger: true,pageSize: size, }" size="small">
            <div slot="speciesId" slot-scope="speciesId">
                <span v-for="(item,i) in specList" :key="item.id">
                    <a-tag v-if="item.id == speciesId" :color="colors[i%13]">{{item.name}}</a-tag>
                </span>
            </div>
            <div slot="serviceProjectId" slot-scope="serviceProjectId">
                <span v-for="(item,i) in projectList" :key="item.id">
                    <a-tag v-if="item.id == serviceProjectId" :color="colors[i%13]">{{item.name}}</a-tag>
                </span>
            </div>
            <div slot="suggestCycle" slot-scope="suggestCycle,record">
                <span v-for="(item,i) in timeUnitList" :key="item.id">
                    <a-tag v-if="item.id == record.timeUnit" :color="colors[i%13]">{{suggestCycle}}{{item.name}}</a-tag>
                </span>
            </div>
            <div slot="status" slot-scope="status">
                <a-switch checked-children="开" un-checked-children="关" disabled :checked="status == 1 ? true : false" />
            </div>
            <div slot="operation" slot-scope="text,record">
                <a-button type="link" @click="update(record)">修改</a-button>
                <a-popconfirm
                    title="是否确认删除当前配置?"
                    ok-text="Yes"
                    cancel-text="No"
                    @confirm="del(record)"
                >
                    <a-button type="link">删除</a-button>
                </a-popconfirm>
            </div>
        </a-table>

        <a-modal
            title="添加、修改宠物服务建议周期配置"
            :visible="visible"
            @ok="handleOk"
            :confirm-loading="confirmLoading"
            @cancel="visible = false"
            >
            <div>
                <a-form :label-col="{ span: 8 }" :wrapper-col="{ span: 14 }">
                    <a-form-item label="宠物种类" >
                        <a-select v-model="item.speciesId" placeholder="请选择宠物种类" allowClear>
                            <a-select-option v-for="(item,i) in specList" :key="i" :value="item.id.toString()">{{item.name}}</a-select-option>
                        </a-select>
                    </a-form-item>
                    <a-form-item label="服务项目" >
                        <a-select v-model="item.serviceProjectId" placeholder="请选择服务项目" allowClear>
                            <a-select-option v-for="(item,i) in projectList" :key="i" :value="item.id.toString()">{{item.name}}</a-select-option>
                        </a-select>
                    </a-form-item>
                    <a-form-item label="周期单位" >
                        <a-select v-model="item.timeUnit" placeholder="请选择周期单位" allowClear>
                            <a-select-option v-for="(item,i) in timeUnitList" :key="i" :value="item.id.toString()">{{item.name}}</a-select-option>
                        </a-select>
                    </a-form-item>
                    <a-form-item label="周期时长">
                        <a-input v-model="item.suggestCycle" placeholder="请输入对应周期单位的周期时长" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="状态">
                        <a-radio-group v-model="item.status" >
                            <a-radio  v-for="(item,i) in statusList" :key="i" :value="item.id">{{item.name}}</a-radio>
                        </a-radio-group>
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>
    </div>
</template>

<script>
import { queryPetServiceSuggestCycle , updatePetServiceSuggestCycle , queryPetServiceProject , queryPetSpecies } from '@/api/pet';
import { columns } from './table';
import moment from 'moment';
export default {
    data () {
        return {
            brandId: this.global.brandId,
            speciesId: undefined,
            specList: [], // 宠物种类列表
            serviceProjectId: undefined,
            projectList: [], // 服务项目列表
            suggestCycle: '', // 建议周期
            timeUnit: undefined, // 周期单位
            timeUnitList: [
                { id: '0', name: '天'},{ id: '1', name: '周'},{ id: '2', name: '月'},{ id: '3', name: '年'},
            ],
            status: undefined, // 状态
            statusList: [
                { id: '0', name: '关闭'},{ id: '1', name: '开启'}
            ],
            colors: ['blue','purple','cyan','green','orange','red','pink','brown','indigo','lime','purple','teal','yellow','amber'],
            columns,
            dataList: [],
            total: 0,
            isload: false,
            page: 1,
            size: 10,
            item: {
                id: '',
                suggestCycle: '',
                speciesId: '',
                serviceProjectId: '',
                timeUnit: '',
                status: '',
            },
            confirmLoading: false,
            isAdd: true,
            visible: false,
        };
    },
    async created(){
        try {
            await this.querySpec();
            await this.queryProject();
            this.getData()
        } catch (error) {
            this.$notification['error']({
                message: '提示',
                description: error,
                duration: 8,
            })
        }
    },
    methods: {
        moment,
        // 查询宠物种类
        querySpec(){
            let _this = this
            return new Promise((resolve,reject) => {
                queryPetSpecies({'size': 99,'page': 0})
                .then(res=>{
                    if(res.success){
                        _this.specList = res.data.content;
                        resolve()
                    }else{
                        reject(res.errMessage)
                    }
                })
            })
        },
        // 查询宠物服务项目
        queryProject(){
            let _this = this
            return new Promise((resolve,reject) => {
                queryPetServiceProject({'size': 99,'page': 0})
                .then(res=>{
                    if(res.success){
                        _this.projectList = res.data.content;
                        resolve()
                    }else{
                        reject(res.errMessage)
                    }
                })
            })
        },
        // 查询数据
        getData(){
            let _this = this
            _this.isload = true
            const params = {
                'brandId': _this.global.brandId,
                'status': _this.status,
                'speciesId': _this.speciesId,
                'serviceProjectId': _this.serviceProjectId,
                'suggestCycle': _this.suggestCycle,
                'timeUnit': _this.timeUnit,
                'size':_this.size,
                'page':_this.page - 1,
            }
            queryPetServiceSuggestCycle(params)
            .then(res=>{
                _this.isload = false
                if(res.success){
                    _this.dataList = res.data.content
                    _this.total = res.data.totalElements
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 分页
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getData()
        },
        // 添加
        add(){
            this.isAdd = true;
            this.item.id = '';
            this.item.speciesId = undefined;
            this.item.serviceProjectId = undefined;
            this.item.timeUnit = undefined;
            this.item.suggestCycle = '';
            this.item.status = '1';
            this.visible = true
        },
        // 修改
        update(item){
            let _this = this;
            _this.item = JSON.parse(JSON.stringify(item))
            _this.isAdd = false;
            _this.visible = true
        },
        // 确认添加、修改
        handleOk(){
            let _this = this
            if(_this.item.speciesId == undefined){
                _this.$message.warning('请选择宠物种类')
                return;
            }
            if(_this.item.serviceProjectId == undefined){
                _this.$message.warning('请选择宠物服务项目')
                return;
            }
            if(_this.item.timeUnit == undefined){
                _this.$message.warning('请选择建议周期单位')
                return;
            }
            if(_this.item.suggestCycle == ''){
                _this.$message.warning('请输入建议周期时长')
                return;
            }
            const params = {
                'id': _this.item.id,
                'speciesId': _this.item.speciesId,
                'serviceProjectId': _this.item.serviceProjectId,
                'suggestCycle': _this.item.suggestCycle,
                'timeUnit': _this.item.timeUnit,
                'status': _this.item.status,
                'brandId': _this.brandId,
            }
            updatePetServiceSuggestCycle(params)
            .then(res=>{
                if(res.success){
                    _this.visible = false;
                    _this.getData();
                    _this.$notification['success']({
                        message: '提示',
                        description: _this.isAdd ? '添加成功' : '修改成功',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 删除
        del(item){
            let _this = this
            const params = {
                'id': item.id,
                'isDelete': '1'
            }
            updatePetServiceSuggestCycle(params)
            .then(res=>{
                if(res.success){
                    _this.getData();
                    _this.$notification['success']({
                        message: '提示',
                        description: '删除成功',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
    }
}

</script>
<style scoped>

</style>